/**
 * @fileOverview
 * Paginate Blogger Posts.
 * 
 * This file is the closure version of the orignal source code.
 * It is under the licence "Apache License 2.0".
 * @author Tzu-yang Tsai
 */

/**
 * @function
 * Setting up the pagination of blogger posts
 * @param {int} [posts_displayed=10] the number of posts displayed on each page.
 * @param {function} display_function the function display the pages. The function has 3 arguments. They are "the current page number", "the array of the links of each page", and "the number of pages". The default value is the inner function defaultDisplayFunction
 */

/**
 * The default function of displaying pagination.
 * {Number} pagenum	Current page number.
 * {Array} pagearray	
 * {Number} numofpages	Number of pages.
 */
function DisplayPaginate(pagenum, pagearray, numofpages){
	var page_div = document.getElementById("blog-pager"),
		styles = {
			each_items : "height: 18px; \
				display:inline-block; \
				border-top-left-radius: 5px; \
				border-bottom-right-radius: 5px; \
				margin: 5px; font-family: cursive, 'comic sans'; \
				font-size: 14px; \
				padding: 3.5px;",
			special_btn : "font-weight: bolder; \
				min-width : 36px; \
				background-color: rgba(204, 201, 255, .75);",
			current_page : "min-width: 18px; \
				background-color: rgb(42, 8, 63); \
				color: rgb(204, 201, 255); ",
			page_link_block : "min-width: 18px; background-color: rgb(204, 201, 255);",
			page_link : "color: rgb(42, 8, 63); \
				text-decoration: none; "
		};
	
	var Maximum_dispalyed = 7;
		
	var page_style_tag = document.createElement("style");
	page_style_tag.innerHTML = ".current-page.page-num{" + styles.current_page + "}" + 
		".page-num{" + styles.each_items + styles.page_link_block + "}" + 
		".page-num a{" + styles.page_link + "}" + 
		".first-page, .last-page{" + styles.special_btn + "}" + 
		".first-page a, .last-page a{" + styles.page_link + "}";
	document.head.appendChild(page_style_tag);
	
	function display_page_blocks(item, i, arr){
		var index = i+1;
		if(pagenum == index){
			//current page
			page_div.innerHTML += "<div class=\"current-page page-num\">" + index + "</div>";
		}else{
			page_div.innerHTML += "<div class=\"page-num\" >" + 
					"<a href = \""+item+"\">" +
						index + 
					"</a>" +
				"</div>";
		}
	}
	
	
	page_div.innerHTML = "";
	if(pagenum != 1){
		page_div.innerHTML += "<div class=\"first-page page-num\">" + 
				"<a href = \""+pagearray[0]+"\">" +
					"First Page" + 
				"</a>" +
			"</div>";
	}
	
	
	if(numofpages > Maximum_dispalyed){	//let it only display 7 pages.
		if(pagenum <= Maximum_dispalyed * 2 / 3){
			for(var i = 0; i < Maximum_dispalyed; i++){
				display_page_blocks(pagearray[i], i, pagearray);
			}
		}else if(pagenum > numofpages - Maximum_dispalyed * 2 / 3){
			for(var i = 0; i < Maximum_dispalyed; i++){
				display_page_blocks(pagearray[i + numofpages - Maximum_dispalyed], i + numofpages - Maximum_dispalyed, pagearray);
			}
		}else{
			for(var i = pagenum - 3; i < pagenum + 2; i++){
				display_page_blocks(pagearray[i], i, pagearray);
			}
		}
	}else{
		pagearray.forEach(function(item, i, arr){
			display_page_blocks(item, i, arr);
		})
	}

	if(pagenum != numofpages){
		page_div.innerHTML += "<div class=\"last-page page-num\">" + 
				"<a href = \""+pagearray[numofpages - 1]+"\">" +
					"Last Page" + 
				"</a>" +
			"</div>";
	}
}
